package com.ln.dm1102.demo04;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author Ln
 * @version 终极版
 * @date 2022/11/2 16:16
 */
public class TestCallable {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建一个Callable类型的任务对象
        MyCallable myCallable = new MyCallable();
        //把任务返回的结果也封装到FutureTask对象中
        FutureTask futureTask = new FutureTask(myCallable);
        //创建一个线程对象，把上面的任务交于该线程对象
        Thread thread = new Thread(futureTask);

        thread.start();
        System.out.println(futureTask.get());
    }
}
class MyCallable implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        int a=0;
        for (int i = 1; i < 101; i++) {
            a+=i;
        }
        return a;
    }
}
